<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>规格属性名称</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
</head>

<body>
    <div style="margin: 15px;padding: 15px;background-color: #fff;">
        <div style="text-align:right">
            <button class="layui-btn layui-btn-xs" onclick="add()">添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>所属类目</th>
                    <th>属性名称</th>
                    <th>操作</th>
                </tr>
            </thead>

            <tbody>
                {foreach $items as $item}
                <tr>
                    <td>{$item['id']}</td>
                    <td>{$item['cate_title']}</td>
                    <td>{$item['title']}</td>
                    <td>
                        <a href="/admins/products/property_values_list?name_id={$item['id']}"
                            class="layui-btn layui-btn-xs layui-btn-primary">属性值</a>
                        <button class="layui-btn layui-btn-xs" onclick="edit({$item['id']})">编辑</button>
                        <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({$item['id']})">删除</button>
                    </td>
                </tr>
                {/foreach}
            </tbody>
        </table>

        <!-- 分页挂载点 -->
        <div id="pages"></div>
    </div>

    <script>
        var $ = layui.jquery;

        layui.use(['laypage', 'util'], function () {
            var laypage = layui.laypage;

            // 分页
            laypage.render({
                elem: 'pages',
                count: '{$total}',
                limit: '{$pageSize}',
                curr: '{$page}',
                layout: ['prev', 'page', 'next', 'limit'],
                jump: function (obj, first) {
                    if (!first) {
                        window.location.href = '?page=' + obj.curr + '&limit=' + obj.limit;
                    }
                }
            });
        });

        function add() {
            layer.open({
                title: '添加属性名称',
                type: 2,//1 dom或文本，2 iframe,3 加载层,4 tips
                maxmin: true,
                area: ['790px', '450px'],
                content: '/admins/products/add_property_name'
            });
        }

        function edit(id) {
            layer.open({
                title: '添加属性名称',
                type: 2,//1 dom或文本，2 iframe,3 加载层,4 tips
                maxmin: true,
                area: ['790px', '450px'],
                content: '/admins/products/edit_property_name?id=' + id
            });
        }

        function del(id) {
            layer.confirm('确定要删除这条吗？', { icon: 3 }, function () {
                $.post('/admins/products/del_property_name', { id }, function (res) {
                    if (res.code) return layer.alert(res.msg, { icon: 2 });
                    layer.alert(res.msg, { icon: 1 });
                    setTimeout(() => window.location.reload(), 1000);
                }, 'json')
            });
        }
    </script>
</body>

</html>